package com.yswl.pri.common;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;

import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebViewClient;
import com.yswl.pri.R;
import com.yswl.pri.home.vedio.utils.WebViewProgressBar;
import com.yswl.pri.home.vedio.utils.X5WebView;

import static com.yswl.pri.home.vedio.AnalysisActivity.lp;
import static com.yswl.pri.home.vedio.AnalysisActivity.lp2;

public class BaseBrowserActivity extends ToolbarActivity {

    /**
     * 作为一个浏览器的示例展示出来，采用android+web的模式
     */
    protected X5WebView mWebView;
    protected WebViewProgressBar mPageLoadingProgressBar = null;
    protected ViewGroup mViewParent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.activity_browser);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setHardware();
        initUI();

    }


    private void initUI() {
        mViewParent = findViewById(R.id.webView1);
        mWebView = new X5WebView(this, null);
        mPageLoadingProgressBar = new WebViewProgressBar(this);// new
        mViewParent.addView(mWebView, lp2);
        mViewParent.addView(mPageLoadingProgressBar, lp);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName(this);
            if (!"com.yswl.pri".equals(processName)) {//判断不等于默认进程名称
                mWebView.setDataDirectorySuffix(processName);
            }
        }
    }

    public String getProcessName(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == android.os.Process.myPid()) {
                return processInfo.processName;
            }
        }
        return null;
    }


    /**
     * 硬件加速
     */
    private void setHardware() {
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getWindow().setFlags(
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception ignored) {
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mWebView.onPause();
        mWebView.stopLoading();
    }

    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.clearCache(true);
            mWebView.destroy();
        }


        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mWebView != null && mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            } else
                return super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }


}
